<?php
/*
	Version   :  1.1
	Created   :  20090806
	Modified  :  20090927
	Author    :  Pablo Ramírez
	co-Author :
	Descr     : Controlador "Price List"
	Changelog : 1.0 - Version Inicial
				1.1 - Control de Acceso
*/
class PriceListsController extends AppController
{
	var $name       = 'PriceLists';
	var $helpers    = array('Html','Javascript','Ajax');
	var $uses		= array('PriceList','Owner','ServiceType', 'Country');
	
	function index() {
		$this->checkSession('admin');
		$this->layout = 'maintenance';
		$ownerArray = array();
		
		/*if (empty($this->data)) {
			$owners_price_list = $this->PriceList->findAll(null,'*','Owner.own_codigo ASC');
			foreach($owners_price_list as $owner_price_list) {
				$temp = array();
				foreach($owner_price_list as $key=>$value) {
					$temp[$key] = $value;
				}
				$owner_price_list_Array[] = $temp;
			}
			$this->set('owners_price_list', $owner_price_list_Array);
		} */
		if (empty($this->data)) {
			//$default_own_codigo = array('--' => 'Select an Owner');
			$owners = $this->Owner->findAll(null,'*','Owner.own_codigo ASC');
			
			foreach($owners as $owner) {
				$temp = array();
				foreach($owner as $key=>$value) {
					$temp[$key] = $value;
				}
				$ownerArray[] = $temp;
			}
			
			/*foreach ($ownerArray as $owner) {
				if (count($owner['PriceList']) > 0) {
					$row['own_nombre'] = $owner['Owner']['own_nombre'];
					foreach ($owner['PriceList'] as $price) {
						$row = 
					
					}
				}
			}*/
			
			//$owns = $this->Owner->generateList();
			$owns = $this->Owner->generateList(null, null, null, '{n}.Owner.own_codigo', '{n}.Owner.own_nombre');

			//$owns = @array_merge($default_own_codigo, $owns);
			$this->set('owners', $ownerArray);
			$this->set('owns', $owns);
			
		} else {
			pr($this->data);
			break;
		
		}
	}
	
	function getPriceList() {
		$this->checkSession('admin');
		//$this->set('datos',$this->data);
		$this->Owner->id = $this->data['Owners']['own_codigo'];
		$this->data = $this->Owner->read();
		
		$service_types_combo_excludes = "";
		$country_service_types = "";
		
		for ($i=0;$i < count($this->data['PriceList']); $i++) {
		//for ($i==0;$i<count($this->data['PriceList']); $i++) {
			$set_codigo = $this->data['PriceList'][$i]['set_codigo'];
			$cou_codigo = $this->data['PriceList'][$i]['cou_codigo'];
			$cou_set_pattern = "'/^.*".$cou_codigo."-".$set_codigo.".*$/'";
			$country_service_types .= $cou_codigo."-".$set_codigo.",";
			
			$this->data['PriceList'][$i]['set_descripcion'] = $this->ServiceType->field("set_descripcion","set_codigo = '$set_codigo'");
			$this->data['PriceList'][$i]['cou_nombre'] = $this->Country->field("cou_nombre","cou_codigo = '$cou_codigo'");
			

			/*if (preg_match($cou_set_pattern, $country_service_types)) {
				$service_types_combo_excludes .= "'".$set_codigo."', ";
			}*/
			

		}
		$service_types_combo_excludes .= "'xx'";
		
		$this->set('own_codigo', $this->Owner->id);
		$this->set('service_types',$this->ServiceType->findAll('set_codigo not like "%--%"'));
		
		$this->set('service_types_combo', $this->ServiceType->generateList("set_codigo not in ($service_types_combo_excludes)",null,null,'{n}.ServiceType.set_codigo', '{n}.ServiceType.set_descripcion' ));
		$this->set('countries', $this->Country->generateList());
		$this->render('owner_price_list','ajax');
	}
	
	
	function add($id = null) {
		$this->checkSession('admin');
		$this->layout = 'ajax';
		//pr($this->data);
		//break;
		if (!empty($this->data)){
			$own_codigo = $this->data['PriceList']['own_codigo'];
			$set_codigo = $this->data['PriceList']['set_codigo'];
			$cou_codigo = $this->data['PriceList']['cou_codigo'];
			$this->PriceList->id = $this->PriceList->field("id_price","PriceList.own_codigo = '$own_codigo' and PriceList.set_codigo = '$set_codigo' and PriceList.cou_codigo = '$cou_codigo'");
			if (!$this->PriceList->save($this->data)){
				$this->set('ERROR', 1);
				$this->set('ERROR_DESC', 'No se pudo guardar el Price');
			}
			$this->Owner->id = $own_codigo;
			$this->data = $this->Owner->read();
			
			$service_types_combo_excludes = "";
			for ($i=0;$i < count($this->data['PriceList']); $i++) {
				$set_codigo = $this->data['PriceList'][$i]['set_codigo'];
				$cou_codigo = $this->data['PriceList'][$i]['cou_codigo'];
				$this->data['PriceList'][$i]['set_descripcion'] = $this->ServiceType->field("set_descripcion","set_codigo = '$set_codigo'");
				//$service_types_combo_excludes .= "'".$set_codigo."', ";
				$this->data['PriceList'][$i]['cou_nombre'] = $this->Country->field("cou_nombre","cou_codigo = '$cou_codigo'");
			}
			$service_types_combo_excludes .= "'xx'";
			$this->set('own_codigo', $this->Owner->id);
			
			$this->set('service_types_combo', $this->ServiceType->generateList("set_codigo not in ($service_types_combo_excludes)",null,null,'{n}.ServiceType.set_codigo', '{n}.ServiceType.set_descripcion' ));
			$this->set('countries', $this->Country->generateList());
			$this->render('owner_price_list','ajax');
		}
	}
	
	function edit($id = null) {
		$this->checkSession('admin');
		$this->layout = 'ajax';
		$this->Owner->id = $id;
		
		if (empty($this->data)){
			$this->data = $this->Owner->read();
			$this->set('id', $id);
		}else{
			if ($this->Owner->save($this->data)){
				//$this->Session->setFlash('Item Editado');
				$this->redirect('/owners');
			} else {
				$this->Session->setFlash('Error al editar el Item');
			}
		}
	}
	
//	function view($id = null) {
//		$this->checkSession('admin');
//		$this->layout = 'ajax';
//		$this->Owner->id = $id;
//		
//		if (empty($this->data)){
			//$this->set('owner', $this->Owner->read());
//			$this->data = $this->Owner->read();
//			$this->set('id', $id);
//		}
//	}
	
	function delPriceList($id, $own_codigo) {
		$this->checkSession('admin');
		//$this->layout = 'ajax';
		if (!$this->PriceList->del($id)) { 
			$this->set('ERROR', '2');
			$this->set('ERROR_DESC', 'No se pudo eliminar el Price');
		} else {
			$this->set('ERROR','0');
			$this->set('ERROR_DESC', 'OK');
			$this->Owner->id = $own_codigo;
			$this->data = $this->Owner->read();
			
			$service_types_combo_excludes = "";
			for ($i=0;$i < count($this->data['PriceList']); $i++) {
				$set_codigo = $this->data['PriceList'][$i]['set_codigo'];
				$cou_codigo = $this->data['PriceList'][$i]['cou_codigo'];
				$this->data['PriceList'][$i]['set_descripcion'] = $this->ServiceType->field("set_descripcion","set_codigo = '$set_codigo'");
				//$service_types_combo_excludes .= "'".$set_codigo."', ";
				$this->data['PriceList'][$i]['cou_nombre'] = $this->Country->field("cou_nombre","cou_codigo = '$cou_codigo'");
			}
			$service_types_combo_excludes .= "'xx'";
			$this->set('own_codigo', $this->Owner->id);
			
			$this->set('service_types_combo', $this->ServiceType->generateList("set_codigo not in ($service_types_combo_excludes)",null,null,'{n}.ServiceType.set_codigo', '{n}.ServiceType.set_descripcion' ));
			$this->set('countries', $this->Country->generateList());
			$this->render('owner_price_list','ajax');
		}
	
	}
}
?>